/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.TextField;

import com.aroundj2me.controller.ApplicationController;
import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.uicomponents.HLine;
import com.aroundj2me.uicomponents.ImageButton;
import com.aroundj2me.utility.ImageLoader;

/**
 * View for doing the searches
 * 
 */
public class SearchView extends Form implements CommandListener,
		ItemCommandListener, IView {
	private ApplicationController controller = null;
	private TextField searchField = null;
	private ImageButton searchButton = null;
	private HLine line = null;
	private final int MAX_LENGTH = 150;

	// commands
	private Command itemPressed = null;

	public SearchView(ApplicationController controller) {
		this();
		this.controller = controller;
	}
	
	public SearchView(){
		super("Search");
		initialize();
		createCommands();
		this.setCommandListener(this);
	}

	/**
	 * Creates the commands
	 * 
	 */
	private void createCommands() {
		itemPressed = new Command("search", Command.OK, 0);
		searchButton.setItemCommandListener(this);
		searchButton.setDefaultCommand(itemPressed);
	}

	/**
	 * initializes the visual components
	 * 
	 */
	private void initialize() {
		Spacer spaceTop = new Spacer(this.getWidth(), this.getHeight() / 4);
		searchField = new TextField("Search", null, MAX_LENGTH, 0);
		searchButton = new ImageButton("Search", ImageLoader
				.getImage(ImageLoader.SEARCH));
		searchButton.setLayout(ImageButton.LAYOUT_CENTER
				| ImageButton.LAYOUT_NEWLINE_AFTER);
		Spacer spaceCenter = new Spacer(this.getWidth(), 5);
		line = new HLine(this.getWidth() - (this.getWidth() / 8));
		line.setLayout(HLine.LAYOUT_NEWLINE_AFTER);
		this.append(spaceTop);
		this.append(searchField);
		this.append(new Spacer(this.getWidth(), 8));
		this.append(searchButton);
		this.append(spaceCenter);
	}

	/**
	 * Action handler
	 */
	public void commandAction(Command c, Item i) {
		if (c == itemPressed && i == searchButton) {
			controller.executeSearch();
		}
	}

	/**
	 * Action handler
	 */
	public void commandAction(Command c, Displayable d) {
		if (c.getCommandType() == Command.BACK) {
			ScreenController.getInstance().last();
		}
	}

	/*
	 * Setters and Getters
	 */
	public TextField getSearchField() {
		return searchField;
	}

	public void setSearchField(TextField searchField) {
		this.searchField = searchField;
	}

	public void setController(ApplicationController controller) {
		this.controller = controller;
	}
}
